package f.h.dragon.howTomcatWorks;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import static f.h.dragon.util.CloseableUtil.*;
import static f.h.dragon.util.OutUtil.*;

/**
 * Socketer: test for socket
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/03/03
 * @since 1.0
 */
public class Socketer {
	
	//local variables
	/**
	 * the host name or ip of server
	 */
	private String host;
	/**
	 * the port of server
	 */
	private int port;
	/**
	 * auto flush
	 */
	private boolean autoflush = true;
	/**
	 * socket
	 */
	private Socket socket;
	
	//Constructor
	/**
	 * 构建指定主机和端口的socket对象
	 * 
	 * @param host - 主机
	 * @param port - 端口
	 */
	public Socketer(String host, int port) {
		this.host = host;
		this.port = port;
		//init socket
		init();
	}
	/**
	 * 构建指定主机、端口和是否自动刷新的socket对象
	 * 
	 * @param host - 主机
	 * @param port - 端口
	 * @param autoflush - 自动刷新，默认为true
	 */
	public Socketer(String host, int port, boolean autoflush) {
		this(host, port);
		this.autoflush = autoflush;
		//init socket
//		init();
	}
	
	//Logic
	/**
	 * 初始化socket连接
	 */
	public void init(){
		try {
			this.socket = new Socket(this.host, this.port);
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 释放socket相关资源
	 */
	public void destroy(){
		try {
			if(this.socket != null){
				this.socket.close();
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 发送信息
	 * 
	 * @param msg
	 * @return
	 */
	public boolean send(String msg){
		OutputStream os = null;
		PrintWriter out = null;
		try {
			os = socket.getOutputStream();
			out = new PrintWriter(os, this.autoflush);
			//send an HTTP request to the web server
			out.println("GET /index.html HTTP/1.1");
			out.println("Host: " + this.host + ":" + this.port);
			out.println("Connection: Close");
			out.println();//请求头与请求实体分隔符：CRLF
//			out.println("...");//请求实体
		} catch (IOException e) {
			throw new RuntimeException(e);
		}finally{
			//释放资源
			close(os);
			close(out);
		}
		return true;
	}
	
	/**
	 * 接收信息
	 * 
	 * @param msg
	 * @return
	 */
	public String receive(){
		BufferedReader in = null;
		try {
			// in stream
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			//read the response
			StringBuilder sb = new StringBuilder(8096);
			if(in.ready()){
				int i = 0;
				while(i != -1){
					i = in.read();
					sb.append((char)i);
				}
			}
			Thread.sleep(50);
			outln(sb.toString());//out msg
			return sb.toString();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}finally{
			close(in);
		}
	}
}
